package sk.fei.vrml.objects;

import sk.fei.vrml.main.Constants;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;

/**
 * VrmlCylinder class represents VRML object Cylinder. Properties are radius,
 * height and three boolean values top, bottom and side. They decide, if that
 * part of cylinder is drawn.
 * 
 * http://www.lighthouse3d.com/vrml/tutorial/index.shtml?cylin
 * 
 * Cylinder {
 *		field SFBool bottom TRUE
 *		field SFFloat height 2
 *		field SFFloat radius 1
 *		field SFBool side TRUE
 *		field SFBool top TRUE
 *	}
 * 
 * @author anubis
 * 
 */
public class VrmlCylinder extends VrmlObject {
	private float radius;
	private float height;
	private boolean side;
	private boolean bottom;
	private boolean top;

	public VrmlCylinder(float radius, float height, boolean side,
			boolean bottom, boolean top) {
		this.radius = radius;
		this.height = height;
		this.side = side;
		this.bottom = bottom;
		this.top = top;
		this.type = Constants.Cylinder;
	}
	
	public void draw(GLAutoDrawable GLAutoDrawable,VrmlAppearance appearance) {    
        GL gl = GLAutoDrawable.getGL();
		GLU glu = new GLU();
        final int slices = 16;
        final int stacks = 16;
        // otocenie aby sa nedaval nalezato a posunutie aby sa tocil okolo svojho stredu
        gl.glRotated(90, 1, 0 ,0);
        gl.glTranslatef(0, 0, -height/2);
        // vykreslenie obalu
        appearance.applyObjectMaterial(GLAutoDrawable);
        if (side){
        	//gl.glColor3f(0.3f, 0.5f, 1f);
            GLUquadric cone = glu.gluNewQuadric();
            appearance.applyTexture(GLAutoDrawable, cone);
        	glu.gluCylinder(cone, radius, radius, height, slices, stacks);
        	glu.gluDeleteQuadric(cone);
        }
        // vykreslenie spodneho uzaveru
        if (bottom){
        	GLUquadric bottomDisk = glu.gluNewQuadric();
        	glu.gluQuadricOrientation 	(	bottomDisk , GLU.GLU_INSIDE );
        	//appearance.applyObjectMaterial(GLAutoDrawable);
            appearance.applyTexture(GLAutoDrawable, bottomDisk);
            //gl.glColor3f(0.6f, 0.2f, 1f);
        	glu.gluDisk(bottomDisk, 0, radius, slices, stacks);
            glu.gluDeleteQuadric(bottomDisk);
        }
        // vykreslenie vrchneho uzaveru
        if (top){
        	GLUquadric upperDisk = glu.gluNewQuadric();
        	//appearance.applyObjectMaterial(GLAutoDrawable);
            appearance.applyTexture(GLAutoDrawable, upperDisk);
            //gl.glColor3f(0.6f, 0.2f, 1f);
            gl.glTranslatef(0, 0, height);
        	glu.gluDisk(upperDisk, 0, radius, slices, stacks);
            gl.glTranslatef(0, 0, -height);
            glu.gluDeleteQuadric(upperDisk);
        }
        if(appearance != null ) {
        	if (appearance.isTextureSeted()) gl.glDisable(GL.GL_TEXTURE_2D);
        }
	}

	public String toString() {
		return "Cylinder";
	}
	
	public float getRadius() {
		return radius;
	}
	
	public void setRadius(float radius) {
		this.radius = radius;
	}
	
	
	public float getHeight() {
		return height;
	}
	
	public void setHeight(float height) {
		this.height = height;
	}
	
	public boolean getSide() {
		return side;
	}
	
	public boolean getBottom() {
		return bottom;
	}
	
	public boolean getTop() {
		return top;
	}
	
	public void setSide(Boolean bool){
		this.side = bool;
	}
	
	public void setBottom(Boolean bool){
		this.bottom = bool;
	}
	
	public void setTop(Boolean bool){
		this.top = bool;
	}

}
